import httpInstance from "@/utils/http";
import qs from "qs";

//获取所有的活动+根据id查找
export function getActivityAllAPI(
  current: number,
  pageSize: number,
  query: string
) {
  return httpInstance({
    url: "/activity/list/all",
    method: "POST",
    data: {
      current,
      pageSize,
      query,
    },
  });
}

//根据活动id获取详细的活动信息
export function getActivityVoByIdAPI(activityId: number) {
  return httpInstance({
    url: "/activity/get/vo",
    method: "GET",
    params: {
      activityId,
    },
  });
}

//根据id搜索活动
export function searchActivityByIdAPI(activityId: number) {
  return httpInstance({
    url: "/activity/get",
    method: "GET",
    params: {
      activityId,
    },
  });
}

//新增社团活动
export function addActivityAPI(data:{
  clubId:number;
  activityName:string;
  activityTitle:string;
  activityContent:string;
  startTime:string;
  endTime:string;
}){
  return httpInstance({
    url:"/activity/add",
    method:"POST",
    data,
  })
}

//修改社团活动信息
export function updateActivityAPI(data:{
  id:number;
  activityName:string;
  activityTitle:string;
  activityContent:string;
  startTime:string;
  endTime:string;
}){
  return httpInstance({
    url:"/activity/update",
    method:"POST",
    data,
  })
}


//获取所有活动申请列表+根据id查找
export function getActivityApplicationAllAPI(
  current: number,
  pageSize: number,
  query?: string | number
) {
  return httpInstance({
    url: "/activity-application/getAll",
    method: "POST",
    data: {
      current,
      pageSize,
      query,
    },
  });
}

//新增活动申请
export function addActivityApplicationAPI(data: {
  userId: number;
  reason: string;
  type: number;
  targetId: number;
  startTime: string;
  endTime: string;
  additionalInfo: {
    activityName: string;
    activityTitle: string;
    activityContent: string;
  };
}) {
  return httpInstance({
    url: "/activity-application/add",
    method: "POST",
    data,
  });
}

//根据id查找活动申请
export function getActivityApplicationByIdAPI(id: number) {
  return httpInstance({
    url: "/activity-application/get",
    method: "GET",
    params: {
      id,
    },
  });
}

//根据社团id获取活动列表
export function listActivityAPI(
  current: number,
  pageSize: number,
  clubId: number
) {
  return httpInstance({
    url: "/activity/list/vo",
    method: "POST",
    data: {
      current,
      pageSize,
      clubId,
    },
  });
}

//单个删除社团活动
export function deleteActivityAPI(ids: number | number[]) {
  const idArray = Array.isArray(ids) ? ids : [ids];

  return httpInstance.delete("/activity/delete", {
    params: { ids: idArray },
    paramsSerializer: {
      indexes: null,
    },
  });
}

//批量删除社团活动申请
export function deleteActivityApplicationAPI(ids: number[]) {
  // 确保ids是数组
  const Ids = Array.isArray(ids) ? ids : [ids];
  
  return httpInstance({
    url: "/activity-application/delete",
    method: "DELETE",
    params: {
      ids: Ids
    },
    paramsSerializer: (params) => {
      return qs.stringify(params, { arrayFormat: "repeat" }); // 生成 ids=1&ids=2 格式
    },
  });
}

//批量删除社团活动
export function deleteUserApplicationAPI(ids: string[]) {
  return httpInstance({
    url: "/activity-application/delete",
    method: "DELETE",
    params: {
      ids: ids,
    },
    paramsSerializer: {
      indexes: null,
    },
  });
}
//查看活动成员列表

interface ListActivityMembersParams {
  current?: number
  pageSize?: number
  sortField?: string
  sortOrder?: 'ascend' | 'descend'
  userId?: number
  activityId?: number
}

// 定义返回数据类型
interface UserVO {
  id: number
  userAccount: string
  userName: string
  userAvatar: string
  phoneNumber: string
  major: string
  grade: string
  userClass: string
  managedClubId: number
  userRole: string
}

interface PageUserVO {
  records: UserVO[]
  total: number
  size: number
  current: number
}

// 封装获取活动成员列表API
export const listActivityMembersAPI = (params: ListActivityMembersParams) => {
  return httpInstance<PageUserVO>({
    url: '/activity/list/user',
    method: 'POST',
    headers: {
      'Content-Type': 'application/json'
    },
    data: {
      current: params.current || 1,
      pageSize: params.pageSize || 10,
      sortField: params.sortField,
      sortOrder: params.sortOrder || 'ascend',
      userId: params.userId,
      activityId: params.activityId
    }
  })
}
// 新增活动成员
export function addActivityMemberAPI(params: { userId: number; activityId: number }) {
  return httpInstance({
    url: "/activity/member/add",
    method: "POST",
    data: params,
    headers: {
      "Content-Type": "application/json"
    }
  });
}

// 移除活动成员
export function deleteActivityMemberAPI(params: { userId: number; activityId: number }) {
  return httpInstance({
    url: "/activity/member/delete",
    method: "POST",
    data: params,
    headers: {
      "Content-Type": "application/json"
    }
  });
}
